<?php
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Helpers/Constants.php");

require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Thumbnails/Thumbnails_In_Folder_Deleter.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Google_Custom_Search/Filter/Valid_Title_Checker.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Keywords_Identifier/Keywords_Identifier.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Thumbnails/Thumbnail_File_Path_Setter.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Thumbnails/Image_To_Thumbnail_Converter.php");

/***********************************************
	This class would filter the search
	results.
************************************************/
class SearchResultsFilter {
	private $title = NULL;
	private $domain = NULL;
	private $link = NULL;
	private $thumbnail = NULL;
	private $keywords = NULL;
	
	private $imageLink = NULL;
	private $isTitleValid = NULL;
	private $thumbnailFilePath = NULL;
	
	private $phpArrayValuesToInsert = NULL;
	private $phpArray = NULL;
	
	/************************************************************/
	
	function __construct() {
		$this->phpArray = array();
		$tifdObj = new ThumbnailsInFolderDeleter(THUMBNAILS_FOLDER_PATH);
	}
	
	/************************************************************/
	
	function filterSearchResults($dataToFilter) {
		foreach ($dataToFilter['items'] as $key) {
			if (is_array($key)) {
				$this->title = $key['title'];
				$this->domain = ucfirst(ltrim($key['displayLink'], "www."));
				$this->link = $key['link'];
				
				foreach ($key as $value) {
					if (is_array($value)) {
						foreach ($value as $subValue) {
							if (is_array($subValue)) {
								foreach ($subValue as $subSubValue) {
									if (is_array($subSubValue)) {
										if (isset($subSubValue['src'])) {
											$this->imageLink = $subSubValue['src'];
										}
									}
								} break;
							}
						} break;
					}
				}
				$vtcObj = new ValidTitleChecker();
				$this->isTitleValid = $vtcObj->checkIsTitleValid($this->title);
				
				if ($this->isTitleValid) {
					$kiObj = new KeywordsIdentifier();
					$this->keywords = $kiObj->getRelevantKeywords($this->link);
					
					if (isset($this->keywords[1])) {
						if (isset($this->imageLink)) {
							$tfpsObj = new ThumbnailFilePathSetter();
							$this->thumbnailFilePath =
								$tfpsObj->setThumbnailFilePath($this->domain,
																THUMBNAILS_FOLDER_PATH,
																$this->imageLink
																);
							
							$this->thumbnailPublicFilePath =
								$tfpsObj->setThumbnailFilePath($this->domain,
																THUMBNAILS_PUBLIC_FOLDER_PATH,
																$this->imageLink
																);
							
							$this->thumbnail = new ImageToThumbnailConverter($this->imageLink,
																				$this->thumbnailFilePath,
																				200, 200
																				);
							
							$this->phpArrayValuesToInsert = array(
															"Title" => $this->title,
															"Domain" => $this->domain,
															"Link" => $this->link,
															"Thumbnail" => $this->thumbnailPublicFilePath,
															"Keywords" => $this->keywords
															);
							
							array_push($this->phpArray, $this->phpArrayValuesToInsert);
						} else continue;
					} else continue;
				} else continue;
			}
		}
		return $this->phpArray;
	}
}
?>